#include "timer.h"

namespace zh
{
    int        Timer::s_initialID {0};
    std::mutex Timer::s_mutex {};

    Timer::Timer(int delayTime, const TimerCallbacks& callback)
    {
        m_id          = generateID();
        m_expiredTime = Clock::now() + std::chrono::milliseconds(delayTime);
        m_callback    = callback;
    }

    int Timer::getID() const
    {
        return m_id;
    }

    TimePoint Timer::getExpiredTime() const
    {
        return m_expiredTime;
    }

    bool Timer::isValid() const
    {
        return m_id != FlagID::INVALID_ID;
    }

    void Timer::run()
    {
        m_callback();
    }

    void Timer::setInvalid()
    {
        m_id = FlagID::INVALID_ID;
    }

    int Timer::generateID()
    {
        int tempID = FlagID::INVALID_ID;
        {
            std::lock_guard<std::mutex> lock(s_mutex);
            ++s_initialID;
            tempID = s_initialID;
        }
        return tempID;
    }
} // namespace zh